package org.adw.launcher2.ld.secret.ui;

import java.util.LinkedList;
import java.util.List;

import org.adw.launcher2.ld.secret.db.MyVideoCache;
import org.adw.launcher2.ld.secret.dom.MyVideo;
import org.adw.launcher2.ld.secret.ui.adapter.MyVideoAdapter;
import org.adw.launcher2.ld.secret.ui.utils.Global;

import org.adw.launcher2.R;
import android.app.Activity;
import android.app.ProgressDialog;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/*
 * Use for test
 */
public class MyHiddenVideoActivity extends Activity {
	public final String TAG = getClass().getSimpleName();
	public MyVideoAdapter videoAdapter;
	private EditText filterText = null;
	boolean isFilter = false;
	public static List<MyVideo> listAfterRestore;
	public ProgressDialog dialog;
	public ListView listVideo;
	public static List<String> listToScan;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_video);
		listVideo = (ListView) findViewById(R.id.listMyVideo);
		Global.cr = getContentResolver();
		// Init db for test something related to database
		filterText = (EditText) findViewById(R.id.txtSearchVideo);
		filterText.addTextChangedListener(watcher);
		loadVideoThread();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		Log.d(TAG, "onDestroy");
		filterText.removeTextChangedListener(watcher);
	}

	private TextWatcher watcher = new TextWatcher() {

		@Override
		public void onTextChanged(CharSequence s, int start, int before,
				int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count,
				int after) {
		}

		@Override
		public void afterTextChanged(Editable arg0) {
			List<MyVideo> listFilter = new LinkedList<MyVideo>();
			for (int i = 0; i < listAfterRestore.size(); i++) {
				String absolutePath = listAfterRestore.get(i)
						.getAbsolutePathNoExtend()
						+ listAfterRestore.get(i).getFileExtend();
				String keyWord = arg0.toString().toLowerCase();

				String[] temp = absolutePath.split("/");
				String fileName = temp[temp.length - 1];

				if (fileName.toLowerCase().contains(keyWord)) {
					listFilter.add(listAfterRestore.get(i));
				}
			}
			bindView(listFilter);
		}
	};

	protected List<MyVideo> loadVideoThread() {
		final List<MyVideo> list = new LinkedList<MyVideo>();
		AsyncTask<Uri, Void, List<MyVideo>> task = new AsyncTask<Uri, Void, List<MyVideo>>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyHiddenVideoActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected List<MyVideo> doInBackground(Uri... uris) {
				return MyVideoCache.getInstance(Global.dbHelper).getVideosNoImageFromDb();
			}

			@Override
			protected void onPostExecute(List<MyVideo> result) {
				listAfterRestore = result;
				Log.d("on Post Exe", "true");
				
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
				bindView(result);
			}
		};
		task.execute();
		Toast.makeText(this, R.string.loading_image, Toast.LENGTH_LONG).show();
		return list;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.my_hidden_contact_menu, menu);// xai chung menu
																// voi contact
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.restore_contact:

			restoreAllSelectedVideos();
			break;
		case R.id.refresh_hidden_contact:
			loadVideoThread();// co the thay bang bindView(listAfterRestore)
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}


	private void restoreAllSelectedVideos() {
		AsyncTask<Uri, Void, Void> task = new AsyncTask<Uri, Void, Void>() {
			@Override
			protected void onPreExecute() {
				dialog = ProgressDialog.show(MyHiddenVideoActivity.this, "",
						"Please wait for few seconds...", true);
			}

			@Override
			protected Void doInBackground(Uri... params) {
				listToScan = new LinkedList<String>();
				for (int i = 0; i < MyVideoAdapter.listToHide.size(); i++) {
					MyVideo myVideo = MyVideoAdapter.listToHide.get(i);
					if(MyNonHiddenVideoActivity.listAfterHide!=null){
						MyNonHiddenVideoActivity.listAfterHide.add(myVideo);
					}
					myVideo.restore();
					String string = myVideo.getAbsolutePathNoExtend()
							+ myVideo.getFileExtend();
					listToScan.add(string);
					Log.d("TAG", listToScan.get(i));
					listAfterRestore.remove(myVideo);
				}
				return null;
			}

			@Override
			protected void onPostExecute(Void result) {
				bindView(listAfterRestore);
				if (dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		};
		task.execute();

	}

	protected void bindView(List<MyVideo> list) {
		videoAdapter = new MyVideoAdapter(this, R.layout.my_video, list);
		listVideo.setAdapter(videoAdapter);

	}
}
